﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace AEM.Controls
{
    /// <summary>
    /// LiftMonitor.xaml 的交互逻辑
    /// </summary>
    public partial class LiftMonitor : UserControl
    {
        private Point _startPoint;
        public LiftMonitor()
        {
            InitializeComponent(); 
        }
        
        private void LiftMonitor_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _startPoint = e.GetPosition(null);
        }

        private void LiftMonitor_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && IsDragging)
            {
                Point position = e.GetPosition(null);
                if (Math.Abs(position.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
                    Math.Abs(position.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
                {
                    this.StartDrag(e);
                }
            }
        }

        private void StartDrag(MouseEventArgs e)
        {
            Point thisPoint = e.GetPosition(null);
            double left = 0, top = 0;
            left = this.Margin.Left;
            top = this.Margin.Top;
            left = thisPoint.X - _startPoint.X + left;
            top = thisPoint.Y - _startPoint.Y + top;
            double maxtop = System.Windows.SystemParameters.WorkArea.Height - this.Height;
            double maxleft = System.Windows.SystemParameters.WorkArea.Width - this.Width;
            if (left > maxleft) left = maxleft;
            if (top > maxtop) top = maxtop;
            if (left < 10) left = 10;
            if (top < 10) top = 10;
            this.Margin = new Thickness(left, top, 0, 0);

            _startPoint = thisPoint;
        }

        public static void OnLiftNameChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                LiftMonitor lm = (LiftMonitor)sender;
                lm.MonitorLiftName.Content = args.NewValue.ToString();
            }
        }

        public static void OnShortNameChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                LiftMonitor lm = (LiftMonitor)sender;
                lm.shortName.Text = args.NewValue.ToString();
            }
        }

        public static void OnLiftStatusChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                LiftMonitor lm = (LiftMonitor)sender;
                lm.MonitorLiftStatus.Style = (Style)args.NewValue;
            }
        }

        public static void OnLiftDirectionChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                LiftMonitor lm = (LiftMonitor)sender;
                lm.LiftTypeAndDdrection.Style = (Style)args.NewValue;
            }
        }

        public static void OnLiftFloorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                LiftMonitor lm = (LiftMonitor)sender;
                lm.MonitorLiftFloor.Text = args.NewValue.ToString();
            }
        }

        public static void OnIsEnableMoveChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                LiftMonitor lm = (LiftMonitor)sender;
                lm.IsDragging = (bool)args.NewValue;
            }
        }

        public static void OnAngleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                RotateTransform rotateTransform = new RotateTransform();
                rotateTransform.CenterX = 45;
                rotateTransform.CenterY = 45;
                rotateTransform.Angle = (int)args.NewValue;

                (sender as LiftMonitor).monitor_model.RenderTransform = rotateTransform;
            }
        }

        #region 注册属性
        public static readonly DependencyProperty LiftNameProperty =
          DependencyProperty.Register("LiftName", typeof(string), typeof(LiftMonitor),
          new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnLiftNameChanged)));

        public string LiftName
        {
            get { return (string)GetValue(LiftNameProperty); }
            set { SetValue(LiftNameProperty, value); }
        }

        public static readonly DependencyProperty ShortNameProperty =
          DependencyProperty.Register("ShortName", typeof(string), typeof(LiftMonitor),
          new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnShortNameChanged)));

        public string ShortName
        {
            get { return (string)GetValue(ShortNameProperty); }
            set { SetValue(ShortNameProperty, value); }
        }

        public static readonly DependencyProperty LiftStatusProperty =
            DependencyProperty.Register("LiftStatus", typeof(Style), typeof(LiftMonitor),
            new FrameworkPropertyMetadata(new PropertyChangedCallback(OnLiftStatusChanged)));

        public Style LiftStatus
        {
            get { return (Style)GetValue(LiftStatusProperty); }
            set { SetValue(LiftStatusProperty, value); }
        }

        public static readonly DependencyProperty LiftDirectionProperty =
            DependencyProperty.Register("LiftDirection", typeof(Style), typeof(LiftMonitor),
            new FrameworkPropertyMetadata(new PropertyChangedCallback(OnLiftDirectionChanged)));

        public Style LiftDirection
        {
            get { return (Style)GetValue(LiftDirectionProperty); }
            set { SetValue(LiftDirectionProperty, value); }
        }

        public static readonly DependencyProperty LiftFloorProperty =
            DependencyProperty.Register("LiftFloor", typeof(string), typeof(LiftMonitor),
            new FrameworkPropertyMetadata("001", new PropertyChangedCallback(OnLiftFloorChanged)));

        public string LiftFloor
        {

            get { return (string)GetValue(LiftFloorProperty); }
            set { SetValue(LiftFloorProperty, value); }
        }

        public static readonly DependencyProperty IsDraggingProperty =
            DependencyProperty.Register("IsDragging", typeof(bool), typeof(LiftMonitor),
            new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnIsEnableMoveChanged)));

        public bool IsDragging
        {
            get { return (bool)GetValue(IsDraggingProperty); }
            set { SetValue(IsDraggingProperty, value); }
        }

        public static readonly DependencyProperty AngleProperty =
            DependencyProperty.Register("Angle", typeof(int), typeof(LiftMonitor),
            new FrameworkPropertyMetadata(0, new PropertyChangedCallback(OnAngleChanged)));

        public int Angle
        {
            get { return (int)GetValue(AngleProperty); }
            set { SetValue(AngleProperty, value); }
        }
        #endregion
    }
}
